<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Article extends Admin_Controller
{
    function __construct ()
    {
        parent::__construct();
        $this->load->model('article_m');
    }
    /**
     * 列表
     * [index description]
     * @return [type] [description]
     */
    public function index($page=1){
        $limit = 20;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['total_rows'] = $this->db->count_all('articles');
        $config['base_url'] = site_url('manage/article/index/');
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $start = ($page-1)*$limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $articles = $this->article_m->get_all_articles($start,$limit);
        if($articles){
            foreach ($articles as $key => $val) {
                $good_category = $this->articlecate_m->get_category_by_id($val['article_category']);
                $articles[$key]['category_name'] = $good_category['category_name'];
            }
        }
        
        $data['articles'] = $articles;
        $this->load->view('articles/index', $data);

    }
    /**
     * 新增
     * [add description]
     */
    public function add()
    {   // 获取频道
        $category = explode('|', $this->config->item('web_category_txt'));
        $categoryId = explode('|', $this->config->item('web_category'));
        $data['category']=array_combine($categoryId,$category);
        // 获取所有商家
        // $shops = $this->shop_m->get_all_shop_users();
        // 获取所有的省
        // $province = $this->areas_m->get_sons_by_pid();
        // $data['province']=$province;
        $data['step'] = 1;
        $data['pic_path'] = "[]";
        $data['pic_con'] = json_encode([]);

        $this->load->view('articles/form',$data);
    }
    /**
     * 修改商品
     * @param  [type]  $good_id [description]
     * @param  integer $step    [description]
     * @return [type]           [description]
     */
    public function edit($article_id,$step=1)
    {
        if ($article_id && $step) {
            $article = $this->article_m->get_articles_by_id($article_id);
            // pp($article);
            // 获取分类

            $data['article'] = $article;
            $data['article_id'] = $article_id;
            $data['step'] = $step;
            $this->load->view('articles/form', $data);
        } else {
            show_message('参数错误',site_url(X_URL.'article/index'),1);
        }

    }
    /**
     * 商品信息
     * [update description]
     * @return [type] [description]
     */
    public function update()
    {
        $post = $this->input->post();
        $article_id = isset($post['article_id']) ? intval($post['article_id']) : false;
        // pp($article_id);exit;
        $article_title = isset($post['article_title']) ? htmlspecialchars($post['article_title'], ENT_QUOTES) : false;
        if (!$article_id) {
            // 新增操作，先添加用户，再添加商品信息
            $article = array(
                'article_title' => $article_title,
                'article_category' => $post['article_category'],
                'article_memo' => $post['article_memo'],
                'article_content' => $post['article_content'],
                'article_add_time' => time(),
                'article_update_time' => time(),
            );
            $articles = $this->article_m->add_articles($article);
            if($articles){
                show_message('新增单页成功',site_url(X_URL.'article/index'),1);
            }else{
                show_message('新增单页失败',site_url(X_URL.'article/edit/'),1);
            }
        } else {
            // 修改单页信息
            $article = array(
                'article_title' => $article_title,
                'article_category' => $post['article_category'],
                'article_memo' => $post['article_memo'],
                'article_content' => $post['article_content'],
                'article_update_time' => time(),
            );
            $affected_rows = $this->article_m->sava_article($article_id,$article);
            if($affected_rows){
                show_message('修改单页成功',site_url(X_URL.'article/index'),1);
            }else{
                show_message('修改单页失败',site_url(X_URL.'article/edit'),1);
            }

        }
    }

    //删除单页
    public function del($id){
        $info = $this->article_m->get_articles_by_id($id);
        $article_cover = $info['article_cover'];
        $this->attachs_m->delete_attachs_by(['access_key'=>$article_cover]);
        $this->article_m->del_article($id);
        show_message('删除文章成功',site_url(X_URL.'article/index'),1);  
    }

    public function search($title='',$order='desc',$page=1){
        $get = $this->input->get();
        $order =!empty($get['order']) ? $get['order'] : $order;
        $keywords = !empty($title) ? $this->mekey->base_decode($title) : $this->input->get('title');
         // pp($keywords);exit;
        if(!$keywords){
            exit;
            // echo json_encode(array('status' => '4','msg' => '抱歉，您没有输入搜索词'));
        }
        if(!get_words_tags_arr($keywords)){
            $sokey = explode(" ",$keywords);
        }else{
            $sokey = !empty($title) ? get_words_tags_arr($this->mekey->base_decode($title)):get_words_tags_arr($keywords);
        }
        $sekey = $this->mekey->base_encode($keywords);
        //分页
        $limit = 30;
        $config['uri_segment'] = 6;
        $config['use_page_numbers'] = TRUE;
        $config['total_rows'] = $this->article_m->count_article_find($sokey);
        $config['base_url'] = site_url('manage/article/search/'.$keywords.'/'.$order);
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $start = ($page-1)*$limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        // $total = $this->user_m->count_user_find($sokey);
        $articles = $this->article_m->article_find($start, $limit, $sokey,$order);
        if($articles){
            foreach($sokey as $s => $m){
                foreach($articles as $k => $v){
                    $articles[$k]['article_title'] = str_replace($m,'<cite class="text-red">'.$m.'</cite>',$v['article_title']);                    
                }
            }
        }
        
        $data['articles']=$articles;
        // pp($data);exit;
        $this->load->view('articles/index',$data);        
    }




}
